<template>
	<view class="container">
		<scroll-view class="list-content" scroll-y>
			<view v-for="(item, index) in listData" :key="index" class="list-item">
				<view class="item-avatar-wrapper">
					<image class="item-avatar" :src="item.user_url"></image>
				</view>
				<view class="item-info">
					<text class="item-nickname">{{item.user_name}}</text>
					<text class="item-position">{{ item.identity === 1 ? '家长' : '小孩' }}</text>
					<text class="item-time">{{timeLoad(item.create_time)}}</text>
					<text class="item-score" v-if="userType == 1">积分：{{item.score}}</text>
				</view>
			</view>
		</scroll-view>
	</view>
	<view><button v-if="userType == 2" @click="onAddBtn">新增父母</button></view>
</template>

<script>
	import {
		publicRelationList
	} from '@/api/api.js'
	import {
		formatDate
	} from '@/utils/time.js'
	import store from '@/store/index.js'; //需要引入store
	export default {
		data() {
			return {
				listData: [],
				userType: store.state.userType
			};
		},
		onLoad() {
			this.loadList()
		},
		methods: {
			onAddBtn() {
				uni.redirectTo({
					url: '/pages/user/user_relation'
				});
			},
			loadList() {
				publicRelationList().then(res => {
					console.log(res.data)
					if (res.data.code != 0) {
						uni.showToast({
							icon: "none",
							title: res.data.message
						})
					} else {
						this.listData = res.data.data
					}
				})
			},
			timeLoad(time) {
				const d = new Date(time * 1000)
				return formatDate(d, 'yyyy-MM-dd')
			},
		}
	};
</script>

<style scoped>
	.container {
		padding: 20px;
	}

	.list-content {
		height: 100%;
	}

	.list-item {
		display: flex;
		align-items: flex-start;
		margin-bottom: 20px;
	}

	.item-avatar-wrapper {
		width: 80px;
		height: 80px;
		border-radius: 50%;
		overflow: hidden;
		margin-right: 20px;
	}

	.item-avatar {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}

	.item-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
	}

	.item-nickname {
		font-size: 18px;
		font-weight: bold;
	}

	.item-position {
		margin-top: 10px;
		font-size: 14px;
		color: #999;
	}

	.item-time {
		margin-top: 10px;
		font-size: 12px;
		color: #999;
	}

	.item-score {
		position: absolute;
		margin-top: 50rpx;
		margin-left: 350rpx;
		color: red;
	}
</style>